{extend name="layout"/}
{block name="css"}
<style>
    .custom-control{margin-left: 18px}
    .pagination{margin: 0; float:right}
</style>
{/block}

{block name="content"}
{if !$modelStatus}
<div class="alert alert-warning">
    <button type="button" class="close" data-dismiss="alert">×</button>
    <i class="fa fa-warning"></i> 此模型已经被冻结不可操作内容
</div>
{/if}
{if empty($fieldArr)}
<div class="alert alert-warning">
    <button type="button" class="close" data-dismiss="alert">×</button>
    <i class="fa fa-warning"></i> 模型必须至少有一个可查询的主表文本字段
</div>
{/if}
<div class="panel">
    <div class="panel-heading">
        <span class="panel-title">独立模型【{$modelTitle}】的内容</span>
        <div class="panel-heading-controls"> 
            <a class="btn btn-xs btn-primary" href="{:url('add',['mid'=>$modelId])}"><i class="fa fa-plus"></i> 新增内容</a>
        </div>
    </div>
    <div class="panel-body">
        <form class="form-inline m-b-2 text-right" method="get">
            <div class="form-group">
                <label class="sr-only" for="form-inline-input-6">搜索关键字</label>
                <input type="text" name="keyword" class="form-control" id="form-inline-input-6" value="{$Request.get.keyword}" placeholder="搜索关键字">
            </div>
            <button type="submit" class="btn btn-primary">搜索</button>
        </form>
        <form action="{:url('delete',['mid'=>$modelId])}" method="post" id="tabledeal">
            <div class="table-responsive">
                <table class="table table-bordered table-hover">
                    <tr>
                        <th width="70">
                            <label class="custom-control custom-checkbox">
                                <input type="checkbox" name="chkAll"  class="custom-control-input" onClick="CheckAll(this.form)">
                                <span class="custom-control-indicator"></span>&nbsp;
                            </label>
                        </th>
                        <th>ID</th>
                         {volist name="$fieldArr" id="vo"}
                               <th>{$vo.title}</th>
                         {/volist}
                        <th width="160">更新时间</th><th>排序</th><th width="60">状态</th><th width="100">操作</th>
                    </tr>
                    {volist name="list" id="vo"}
                    <tr id="{$vo.id}">
                        <td>
                            <label class="custom-control custom-checkbox">
                                <input type="checkbox" name="ids[]"  class="custom-control-input" value="{$vo.id}">
                                <span class="custom-control-indicator"></span>&nbsp;
                            </label>
                        </td>
                        <td>{$vo.id}</td>
                         {volist name="$fieldArr" id="v"}
                               <td>{$vo[$v.name]}</td>
                         {/volist}
                        <td>{:date('Y-m-d H:i:s',$vo.update_time)}</td>
                        <td><table class="editable-table" width="100%" height="100%"><tr><td>{$vo.orders}</td></tr></table></td>
                        <td>
                            {if $vo.status} 
                            <button  data-id="0" type="button" class="btn btn-sm btn-primary btn-rounded status">启用</button>
                            {else}
                            <button  data-id="1"  type="button" class="btn btn-sm btn-default btn-rounded status">禁用</button>
                            {/if}
                        </td>
                        <td><a href="{:url('edit',['mid'=>$modelId,'id'=>$vo.id])}"><i  class="fa fa-edit btn  btn-sm btn-primary" data-toggle="tooltip" data-placement="top"  data-original-title="编辑"></i></a> <i  class="fa fa-times btn  btn-sm btn-warning del" data-toggle="tooltip" data-placement="top"  data-original-title="删除"></i></td>
                    </tr>
                    {/volist}
                    <tr>
                        <td>
                            <button class="btn" type="button" id="del">删除 </button>
                        </td>
                        <td colspan="8">
                              {$page|raw}
                        </td>
                    </tr>
                </table>
            </div>
        </form>
    </div>
</div>
{/block}

{block name="javascript"}
<script>
    var modelId='{$modelId}';
    function CheckAll(form) {
        for (var i = 0; i < form.elements.length; i++) {
            var e = form.elements[i];
            if (e.Name !== "chkAll" && e.disabled !== true)
                e.checked = form.chkAll.checked;
        }
    }
    $(function () {
        $('[data-toggle="tooltip"]').tooltip();
         //排序
        $('.editable-table').each(function () {
            $(this).editableTableWidget({
                editor: $('<input class="form-control">')
            });
        });
        $('.editable-table td').on('change', function (evt, newValue) {
            var id = $(this).parent().parent().parent().parent().parent().attr('id');
            var ifchange = false;
            $.ajax({
                async: false,
                url: "{:url('changeorder')}",
                type: "GET",
                data: {
                    mid:modelId,
                    id: id,
                    num: newValue
                },
                dataType: 'text',
                success: function (data) {
                    if (data === 'true') {
                        ifchange = true;
                        // alert(ifchange);
                    } else {
                        toastr['warning'](data, '', {
                            closeButton: true
                        });
                    }
                }
            });
            return ifchange;
        });
        $('#del').click(function () {
            bootbox.confirm({
                message: '确认删除吗？删除后不可恢复',
                className: 'bootbox-sm',
                callback: function (result) {
                    if (result) {
                        $('#tabledeal').submit();
                    }
                }
            });
        });
        $('.status').click(function () {
            var nowdom = $(this);
            var id = nowdom.parent().parent().attr('id');
            var status = nowdom.attr('data-id');
            $.ajax({
                url: "{:url('setstate')}",
                type: "GET",
                data: {
                    mid: modelId,
                    id: id,
                    status: status
                },
                dataType: 'text',
                success: function (data) {
                    if (data === 'true') {
                        if (status == 1) {
                            nowdom.addClass('btn-primary');
                            nowdom.attr('data-id', '0');
                            nowdom.html('启用');
                        }
                        if (status == 0) {
                            nowdom.removeClass('btn-primary');
                            nowdom.attr('data-id', '1');
                            nowdom.html('禁用');
                        }
                    } else {
                        toastr['warning'](data, '', {
                            closeButton: true
                        });
                    }
                }
            });
        });

        $('.del').click(function () {
            var nowdom = $(this).parent().parent();
            bootbox.confirm({
                message: '确认删除吗？删除后不可恢复',
                className: 'bootbox-sm',
                callback: function (result) {
                    if (result) {
                        var id = nowdom.attr('id');
                        $.ajax({
                            url: "{:url('delete')}",
                            type: "GET",
                            data: {
                                mid: modelId,
                                id: id
                            },
                            dataType: 'text',
                            success: function (data) {
                                if (data === 'true') {
                                    nowdom.remove();
                                    toastr['success']('信息删除成功~', '', {
                                        closeButton: true
                                    });
                                } else {
                                    toastr['warning'](data, '', {
                                        closeButton: true
                                    });
                                }
                            }
                        });
                    }
                }
            });
        });
    });
</script>
{/block}